layui.define(['element', 'api', 'amis', 'schema', 'listener', 'form', 'utils', 'schema_render'], function(exports) {
	"use strict";

	/**
	 * Button component
	 * */
	var MOD_NAME = 'resource_properties',
		$ = layui.jquery,
		api = layui.api,
        amis = layui.amis,
        schema = layui.schema,
        form = layui.form,
        listeners = layui.listener,
        laytpl = layui.laytpl,
        schema_render = layui.schema_render,
        utils = layui.utils,
        metaType = null,
        selectFileMetaData,
        elem = null,
        path = null,
        treeNode = null,
        schemaType=null,
        resource_tree_obj,

        propertiesPanelListeners = {
            domain_model_propertiesPanel_listener:domain_model_propertiesPanel_listener,
            domain_model_field_propertiesPanel_listener:domain_model_field_propertiesPanel_listener,
            domain_file_field_primary_key_propertiesPanel_listener:domain_file_field_primary_key_propertiesPanel_listener
        }
    ;





    /**
     * 属性面板_单击树监听器
     * @param {} event 
     * @param {*} treeId 
     * @param {*} treeNode 
     */
     function properties_panel_resourceTreeClickListener(event, treeId, treeNode, option, pearBody, resource_tabs_obj){
        var treeObj = $.fn.zTree.getZTreeObj(treeId)
        var schemaType = treeObj.setting.view.schemaType;

        var metaType = treeNode.data.type.metaType;
        var resourceTypeSchema = schema.get(schemaType);
        var typeSchema = resourceTypeSchema[metaType];

        if(typeSchema.propertiesPanel){
            layui.resource_properties.render({
                elem: '#viewForm',
                path: treeNode.data.path,
                treeNode: treeNode,
                schemaType:schemaType,
                resource_tree_obj: resource_tree_obj
            })
        }else{
        //    移除渲染DOM
        // 先注释掉，解决点击右侧页面操作列不生效的问题
            // $('#viewForm').remove();
        }
    }

     
    /**
     * 模块装配
     */
     function configur(){
        listeners.registerResourceTreeClickListener('properties_panel_resourceTreeClickListener', properties_panel_resourceTreeClickListener);
    }
    
    

    

    form.on('submit(*)', function(data){
        var fileData = $.extend({}, selectFileMetaData.metadata, data.field);


        api.put({
            enableRootPath: true,
            url: "schemas/" + selectFileMetaData.selectData.data.type.metaType,
            data: {path:selectFileMetaData.selectData.data.path, value:JSON.stringify(fileData)},
            async: false,
            // dataType: "json",
            success: function(data){
                layer.msg('保存成功');
                resource_tree_obj.selectedNode({
                    id: selectFileMetaData.selectData.id
                });
            }
        });

        // schemas
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    });


    function domain_model_propertiesPanel_listener(data){
        getFileMetaData(path, treeNode);
    }
    function domain_model_field_propertiesPanel_listener(data){
        getFileMetaData(path, treeNode);
    }
    function domain_file_field_primary_key_propertiesPanel_listener(data){
        getFileMetaData(path, treeNode);
    }

	var resource_properties = function(opt) {
		this.option = opt;
	};


    function randerForm(script, data) {
        var getTpl = script.innerHTML,view = document.getElementById(elem.replace('#', ''));
        laytpl(getTpl).render(data, function(html){
            view.innerHTML = html;
        });
    }


    function resetForm(){
        $("#form-data")[0].reset();
        form.render();
    }

    function getFileMetaData(path, selectData) {
        randerForm(utils.schemas.formType[selectData.data.type.metaType], selectData.data)
        api.get({
            enableRootPath: true,
            url: "resource",
            data: {path:path},
            async: false,
            dataType: "json",
            success: function(data){
                selectFileMetaData = {
                    metadata: data,
                    selectData: selectData
                }

                //重置表单
                resetForm();
                //给表单赋值
                form.val('data-form',selectFileMetaData.metadata)

            }
        });
    }

    function getDomId(contentElem){
        var domId = contentElem.slice(1) + "_container";
        return domId;
    }

    function resetContent(contentElem){


        var domId = getDomId(contentElem);
        var dom = `
            <div id="${domId}">
                <fieldset class="layui-elem-field layui-field-title">
                    <legend><i class="layui-icon layui-icon-app" style="font-size: 26px; color: #1E9FFF;"></i>    Properties</legend>
                </fieldset>
                <div id="${contentElem.slice(1)}">
    
                </div>    
            </div>

        `


        $('#' + domId).remove();
        $('#viewFormContainer').append(dom)
    }

    function removePropertiesDom(contentElem){
        var domId = getDomId(contentElem);
        $('#' + domId).remove();
    }


    /**
	 * Button start loading
	 * */
    resource_properties.prototype.render = function(opt) {
		
		var option = {
			elem: opt.elem,
            path: opt.path,
            treeNode: opt.treeNode,
            amisData: opt.amisData,
            schemaType:opt.schemaType,
            resource_tree_obj: opt.resource_tree_obj
		}

        elem = option.elem;
        metaType = option.treeNode.data.type.metaType;
        path = option.path;
        treeNode = option.treeNode;
        resource_tree_obj = option.resource_tree_obj;
        // schemaType = option.schemaType;

        var resourceTypeSchema = schema.getResource(option.schemaType);
        var typeSchema = resourceTypeSchema[metaType];


        if(typeSchema.propertiesPanel){
            resetContent(option.elem);
            var id = typeSchema.id;
            var propertiesPanel = typeSchema.propertiesPanel;
            if(propertiesPanel.mode === 'listener'){
                var listenerName;
                if(propertiesPanel.listenerName){
                    listenerName = propertiesPanel.listenerName;
                }else{
                    listenerName = id + '_propertiesPanel_listener';
                }
                var listener = propertiesPanelListeners[listenerName];
                if(listener &&  typeof listener === 'function'){
                    listener(option.treeNode)
                }

            }
            else{
                schema_render.render({
                    elem: option.elem,
                    schema: propertiesPanel,
                    contextData: option.treeNode
                })
            }
            //
            // else if(propertiesPanel.mode === 'amis'){
            //     debugger
            //     // var allData = getGlobalEnv(option.treeNode.data);
            //     var amisSchema = propertiesPanel.amisSchema;
            //     var amisSchemaApi = propertiesPanel.amisSchemaApi;
            //     amis.render({
            //         elem: option.elem,
            //         schema: amisSchema,
            //         schemaApi: amisSchemaApi,
            //         props:{
            //             data:option.treeNode
            //         }
            //     })
            // }else if(propertiesPanel.mode === 'iframe'){
            //     var iframeUrl = propertiesPanel.url;
            //     showIframe(iframeUrl);
            // }
        }else{
            removePropertiesDom(option.elem);
        }


        return new resource_properties(option);
	}


    configur();
    exports(MOD_NAME, new resource_properties());
});
